package mainFragment.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseFragment;
import common.Constant;
import https.InterNetController;
import in.srain.cube.views.ptr.PtrFrameLayout;
import mainFragment.activity.CollegeMoreDetail;
import mainFragment.adapter.ZhaoShengMessageListAdapter;
import mainFragment.bean.ZhaoShengMessageBean;
import utils.MyLogUtils;
import view.MyPtrRefresh_LoadView;

/**
 * Created by Administrator on 2017/3/3.
 */
@SuppressLint("ValidFragment")
public class ZhaoShengMessage extends BaseFragment {
    private View view;
    private Context context;
    private String id;
    private MyPtrRefresh_LoadView college_fragment_zsm_refresh_load_layout;
    private ListView college_fragment_zsm_rl_lv;
    private ZhaoShengMessageListAdapter adapter;
    private ArrayList<ZhaoShengMessageBean.NewsListBean> arrayList;
    private ZhaoShengMessageBean zhaoShengMessageBean;
    private int page;//当前页数
    private final int REFRESH = 01;
    private final int LOAD = 02;

    private Handler ZhaoShengMessageHandler;
    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    Gson gson = new Gson();
                    zhaoShengMessageBean = gson.fromJson((String) msg.obj, ZhaoShengMessageBean.class);
                    if (zhaoShengMessageBean != null) {
                        List<ZhaoShengMessageBean.NewsListBean> newsList = zhaoShengMessageBean.getNewsList();
                        if (newsList != null) {
                            if (newsList.size() > 0) {
                                for (int i = 0; i < newsList.size(); i++) {
                                    switch (msg.arg1) {
                                        case REFRESH:
                                            arrayList.add(0, newsList.get(i));
                                            break;
                                        case LOAD:
                                            arrayList.add(newsList.get(i));
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                            college_fragment_zsm_refresh_load_layout.OnRefreshComplete();
                            if (adapter != null) {
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                    break;
                case Constant.ERROR_CODE_FINISH:
                    college_fragment_zsm_refresh_load_layout.OnRefreshComplete();
                    break;
                default:
                    break;
            }
        }
    };

    public ZhaoShengMessage(Context context, String id, Handler handler) {
        this.id = id;
        this.context = context;
        ZhaoShengMessageHandler = handler;

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.college_fragment_zsm, null, false);
        }
        if (getMyActivity() != null) {
            initView();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (ZhaoShengMessageHandler != null) {
            Message message = new Message();
            message.what = CollegeMoreDetail.ZHAOSHENGMESSAGE_COMPLETE;
            ZhaoShengMessageHandler.sendMessage(message);
        }
        page = 1;
        return view;
    }

    private void initView() {
        page = 1;
        college_fragment_zsm_refresh_load_layout = (MyPtrRefresh_LoadView) view.findViewById(R.id.college_fragment_zsm_refresh_load_layout);
        college_fragment_zsm_rl_lv = (ListView) view.findViewById(R.id.college_fragment_zsm_rl_lv);
        arrayList = new ArrayList<>();
        adapter = new ZhaoShengMessageListAdapter(getMyActivity(), arrayList);
        college_fragment_zsm_rl_lv.setAdapter(adapter);
        college_fragment_zsm_refresh_load_layout.setMyPtrHandler(new MyPtrRefresh_LoadView.MyPtrHandler() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                page++;
                getZhaoShengMessageFromInternet(page, LOAD);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page++;
                getZhaoShengMessageFromInternet(page, REFRESH);
            }
        });
    }

    public void setZhaoShengMessageBean(ZhaoShengMessageBean collegeBean) {
        if (arrayList != null) {
            arrayList.clear();
            if (collegeBean == null) {
                getZhaoShengMessageFromInternet(page, REFRESH);
            } else {
                List<ZhaoShengMessageBean.NewsListBean> newsList = collegeBean.getNewsList();
                if (newsList != null) {
                    for (int i = 0; i < newsList.size(); i++) {
                        arrayList.add(newsList.get(i));
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }

    private void getZhaoShengMessageFromInternet(int page, int flag) {
        if (id != null) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("college_id", id);
            hashMap.put("page", String.valueOf(page));
            new InterNetController(getMyActivity(), Constant.NEWLIST, myHandler, hashMap, flag, false);
        }
    }
}
